package com.seecen.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seecen.mapper.RecipeCategoryRelationMapper;
import com.seecen.mapper.RecipesMapper;
import com.seecen.pojo.RecipeCategoryRelation;
import com.seecen.pojo.Recipes;
import com.seecen.service.RecipeCategoryRelationService;
import com.seecen.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 19811
* @description 针对表【t_recipe_category_relation(菜谱分类关联表)】的数据库操作Service实现
* @createDate 2025-07-22 09:54:27
*/
@Service
public class RecipeCategoryRelationServiceImpl extends ServiceImpl<RecipeCategoryRelationMapper, RecipeCategoryRelation>
    implements RecipeCategoryRelationService{

    @Autowired
    private RecipesMapper recipesMapper; // 注入 RecipesMapper

    @Override
    public Result findRecipesByCategoryId(Integer categoryId) {
        // 1. 查询关联的菜谱ID列表
        List<Integer> recipeIds = baseMapper.selectRecipeIdsByCategoryId(categoryId);
        if (recipeIds.isEmpty()) {
            return Result.ok(Collections.emptyList()); // 直接返回空结果
        }

        // 2. 使用 recipesMapper 查询菜谱详情
        List<Recipes> recipes = recipesMapper.selectBatchIds(recipeIds);
        if (recipes.isEmpty()) {
            return Result.ok(Collections.emptyList());
        }

        // 3. 转换前端需要的格式
        List<Map<String, Object>> result = recipes.stream().map(recipe -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", recipe.getRecipesId());
            map.put("name", recipe.getRecipesName());
            map.put("cover", recipe.getRecipesUrl()); // 封面图片URL
            map.put("ingredients", recipe.getFoodMaterials());
            map.put("favoriteCount", 0); // 示例值，实际应从其他表查询
            return map;
        }).collect(Collectors.toList());

        return Result.ok(result); // 返回统一格式的结果

    }
}




